/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ytmc;

/**
 *
 * @author K
 */
public class TransferSpeed
{
    private long bytesPerTimeUnit;

    public long getBytesPerSec()
    {
        return bytesPerTimeUnit;
    }

    public void setBytesPerSec(long bytesPerTimeUnit)
    {
        this.bytesPerTimeUnit = bytesPerTimeUnit;
    }

    public TransferSpeed(long bytesPerTimeUnit)
    {
        this.bytesPerTimeUnit = bytesPerTimeUnit;
    }

        @Override
    public String toString()
    {
        return toString( TransferSpeed.SizeUnit.AUTO, 2, TransferSpeed.TimeUnit.S );
    }

    public String toString( TransferSpeed.SizeUnit sizeUnit )
    {
        return toString( sizeUnit, 2, TransferSpeed.TimeUnit.S );
    }

    public String toString( TransferSpeed.SizeUnit sizeUnit, TransferSpeed.TimeUnit timeUnit )
    {
        return toString( sizeUnit, 2, timeUnit );
    }

    public String toString( int decimalPlaces )
    {
        return toString( TransferSpeed.SizeUnit.AUTO, decimalPlaces, TransferSpeed.TimeUnit.S );
    }

    public String toString( TransferSpeed.SizeUnit sizeUnit, int decimalPlaces, TransferSpeed.TimeUnit timeUnit )
    {
        double dSize = bytesPerTimeUnit;
        String strUnit = sizeUnit.toString();


        if( decimalPlaces < 0 || decimalPlaces > 6 )
        {
            decimalPlaces = 0;
        }

        switch( sizeUnit )
        {
            case B: break;
            case KB: dSize /= 1024; break;
            case MB: dSize /= Math.pow( 1024, 2 ); break;
            case GB: dSize /= Math.pow( 1024, 3 ); break;

            case AUTO:
            {
                int i = 0;
                strUnit = FileSize.Unit.values()[i+1].toString();

                while( dSize >= 1024 && i <= 3 )
                {
                    dSize /= 1024;
                    i++;
                    strUnit = FileSize.Unit.values()[i+1].toString();
                }
                break;
            }
        }

        String strSize = String.format( "%." + decimalPlaces + "f", dSize );
        strSize = strSize.replaceFirst( ",?0+$", "" );
        return strSize + " " + strUnit + "/" + timeUnit.toString().toLowerCase();
    }

    public enum SizeUnit
    {
        AUTO, B, KB, MB, GB
    }

    public enum TimeUnit
    {
        S, MIN, H
    }
}